﻿using Basic.FormModel.DB;
using Basic.FormModel.FormViewCol;
using LinqKit;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class FormViewColDAL : BasicDAL<DBFormViewCol, long>, IFormViewColDAL
    {
        public FormViewColDAL ( IRepository<DBFormViewCol> basicDAL ) : base(basicDAL, "view.column")
        {
        }
        public long Add ( FormViewColAdd add )
        {
            DBFormViewCol data = add.ConvertMap<FormViewColAdd, DBFormViewCol>();
            data.Id = IdentityHelper.CreateId();
            this._BasicDAL.Insert(data);
            return data.Id;
        }

        public Result[] Adds<Result> ( FormViewColAdd[] data ) where Result : class, new()
        {
            DBFormViewCol[] adds = data.ConvertMap<FormViewColAdd, DBFormViewCol>();
            adds.ForEach(c =>
            {
                c.Id = IdentityHelper.CreateId();
            });
            base._BasicDAL.Insert(adds);
            return adds.ConvertMap<DBFormViewCol, Result>();
        }
        public Result[] Sync<Result> ( FormViewColAdd[] data, long[] ids ) where Result : class, new()
        {
            if ( data.IsNull() )
            {
                _ = this._BasicDAL.Delete(a => ids.Contains(a.Id));
                return Array.Empty<Result>();
            }
            DBFormViewCol[] adds = data.ConvertMap<FormViewColAdd, DBFormViewCol>();
            adds.ForEach(c =>
            {
                c.Id = IdentityHelper.CreateId();
            });
            ISqlQueue<DBFormViewCol> queue = this._BasicDAL.BeginQueue();
            queue.Delete(a => ids.Contains(a.Id));
            queue.Insert(adds);
            _ = queue.Submit();
            return adds.ConvertMap<DBFormViewCol, Result>();
        }
    }
}
